﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerNationalitieDao : INationalitieDao
    {
        public SqlServerNationalitieDao()
        {
            SQLUtility.TableName = "Nationalitie";
        }

        //Trả về đối tượng báo ăn
        private Nationalitie MakeNationalitie(DataRow row)
        {
            Guid _id = Guid.Parse(row["NationalitiesID"].ToString());
            string _nationalitiesNumbe = row["NationalitiesNumber"].ToString();
            string _nationalitiesName = row["NationalitiesName"].ToString();


            return new Nationalitie(_id, _nationalitiesNumbe, _nationalitiesName);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<Nationalitie> MakeNationalities(DataTable dt)  
        {
            IList<Nationalitie> list = new List<Nationalitie>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeNationalitie(row));
            }
            return list;
        }
        private DbParameter[] Take(Nationalitie Nationalitie)
        {
            DbParameter[] parameters = new DbParameter[3];
            parameters[0] = new SqlParameter("@NationalitiesID", Nationalitie.Id);
            parameters[1] = new SqlParameter("@NationalitiesNumber", Nationalitie.NationalitiesNumber);
            parameters[2] = new SqlParameter("@NationalitiesName", Nationalitie.NationalitiesName);
            return parameters;
        }
        public IList<Business_Objects.Nationalitie> GetNationalitie()
        {
            SQLUtility.TableName = "Nationalitie";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeNationalities(dt); 
        }

        public Business_Objects.Nationalitie GetNationalitieByID(string NationalitieID)
        {
            SQLUtility.TableName = "Nationalitie";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@NationalitiesID", NationalitieID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeNationalitie(row);
            }
        }

        public void InsertNationalitie(Business_Objects.Nationalitie Nationalitie)
        {
            SQLUtility.TableName = "Nationalitie";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(Nationalitie), true);
        }

        public int UpdateNationalitie(Business_Objects.Nationalitie Nationalitie)
        {
            SQLUtility.TableName = "Nationalitie";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(Nationalitie)); }
            catch { return -1; }
        }

        public int DeleteNationalitie(Business_Objects.Nationalitie Nationalitie)
        {
            SQLUtility.TableName = "Nationalitie";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(Nationalitie)); }
            catch { return -1; }
        }
    }
}
